<template>
    <div class="goodslist-box">
        <div class="goods-list">

            <div class="goods-item" v-for="item in goodsList" :key="item.id" @click="getGoodsInfo(item.id)">
                <img :src="item.img_url" alt="图片加载失败">    
                <h1>{{item.title}}</h1>
                <div class="goods-info">
                    <div class="goods-price">
                        <span class="new-price">&yen;{{item.market_price}}</span><span class="old-price">&yen;{{item.sell_price}}</span>
                    </div>

                    <div class="sell">
                        <span>热卖中</span>
                        <span>剩余{{item.stock_quantity}}件</span>
                    </div>
                </div>
            </div>       
        </div>
        <!-- 加载更多 -->
        <mt-button type="danger" size="large" class="btn" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';

export default {
    data () {
        return {
            pageindex: 1, // 商品列表页码默认为 1
            goodsList: [] // 商品列表数据
        }
    },
    created() {
        this.getGoodsList()
    },  
    methods: {

        getGoodsList(){
            this.$http.get('api/getgoods?pageindex=' + this.pageindex).then(res=>{
                // console.log(res);
                if (res.body.status === 0) {
                    this.goodsList = this.goodsList.concat(res.body.message);
                } else {
                    Toast('获取商品列表失败');
                }
            })
        },

        getMore(){
            this.pageindex++,
            this.getGoodsList()
        },

        getGoodsInfo(id) {
            // console.log(this);
            // console.log(id);
            // console.log(1123);
            this.$router.push({ name: "getgoodsinfo", params: { id } });
        }
    }
}
</script>

<style lang="less" scoped>
    .goodslist-box {
        .goods-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            padding: 10px;
            .goods-item {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                margin: 4px 0;
                border: 1px solid #ccc;
                width: 49%;
                box-shadow: 0 0 5px #999;
                padding: 2px;
                min-height: 293px;
                img {
                    width: 100%;
                }

                h1 {
                    // padding: 3px;
                    font-size: 14px;
                }
                .goods-info {
                    background-color: #eee;
                    div {
                        padding: 7px;
                    }
                    .goods-price {
                        .new-price {
                            font-size: 16px;
                            font-weight: 600;
                            color: #f06;
                        }
                        .old-price {
                            margin-left: 15px;
                            text-decoration: line-through;
                            font-size: 13px;
                            color: #666;
                        }
                    }
                    .sell {
                        display: flex;
                        justify-content: space-between;
                        font-size: 13px;
                        color: #666;
                    }
                }
            }
        }
    }

    .btn {
        margin-bottom: 10px;
    }
</style>
